package com.coolingme.general;

import java.util.HashMap;
import java.util.Map;

/**
 * 享元工厂
 * 注册式单例，但享元模式的重点在结构上，而不是创建对象上
 *
 * @author wangyue
 * @date 2020-09-16 22:42
 */
public class FlyweightFactory {

    /**
     * 缓存池
     */
    private static Map<String, IFlyweight> pool = new HashMap<>();

    /**
     * 内部状态具备不变性，因此作为缓存的键
     *
     * @param intrinsicState 内部状态
     * @return 享元角色
     */
    public static IFlyweight getFlyweight(String intrinsicState) {
        if (!pool.containsKey(intrinsicState)) {
            IFlyweight flyweight = new ConcreteFlyweight(intrinsicState);
            pool.put(intrinsicState, flyweight);
        }
        return pool.get(intrinsicState);
    }

}
